package com.tangqi.android;

import java.util.ArrayList;
import java.util.List;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class SQLiteDemo extends Activity {
    private static final String TAG = "SQLiteDemo";
	/** Called when the activity is first created. */
    private TextView tvName = null;
    private TextView tvPhone = null;
    
	private Button searchBtn;
	private Button resetBtn;
	private Button addBtn;
	
	private Button updateBtn;
	private Button deleteBtn;
	
	private TableLayout tblLayout;	
	private List<Person> personList = null;
	
	private LinearLayout.LayoutParams TV_LP = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);  
	private LinearLayout.LayoutParams TR_LP = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);  

	private List<Integer> personIdList = new ArrayList<Integer>();
//	private Integer personId ;
	
	PersonService personService = null;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        tvName = (TextView)findViewById(R.id.name);
        tvPhone = (TextView)findViewById(R.id.phone);
        
        tblLayout = (TableLayout) findViewById(R.id.tableLayout1);
        personService = new PersonService(this.getBaseContext());
               
        searchBtn = (Button) findViewById(R.id.searchBtn);
        searchBtn.setOnClickListener(listener);
        
        resetBtn = (Button) findViewById(R.id.resetBtn);
        resetBtn.setOnClickListener(listener);
        
        addBtn = (Button) findViewById(R.id.addBtn);
        addBtn.setOnClickListener(listener2);
        
        updateBtn = (Button) findViewById(R.id.updateBtn);
        updateBtn.setOnClickListener(listener2);
        
        deleteBtn = (Button) findViewById(R.id.deleteBtn);
        deleteBtn.setOnClickListener(listener2);
        
    }
    
    private OnClickListener listener=new OnClickListener(){
		@Override
		public void onClick(View v){
			Button btn=(Button)v;
			Log.i(TAG, btn.getId()+" is clicked");
			switch (btn.getId()){
			  case R.id.searchBtn: addToTable(tblLayout,personList);break;					
			  case R.id.resetBtn:clnResultAndCondition(tblLayout);break;
			}
		}
	};
	
	private OnClickListener listener2 = new OnClickListener() {
		public void onClick(View v) {
			Button btn = (Button) v;
//			Intent intent = new Intent();
			switch (btn.getId()) {
				case R.id.addBtn:add();break;
				case R.id.updateBtn:gotoUpdateActivity();break;
				case R.id.deleteBtn:delete();break;
			}
//			startActivity(intent);
		}
	};
	
	private OnCheckedChangeListener ocListener=new OnCheckedChangeListener(){
		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			int personId = buttonView.getId();
			// TODO Auto-generated method stub
			if(isChecked){
				personIdList.add(personId);
			}else{
				personIdList.remove(personId);
			}
		}
	};
	
	public void add(){
		Intent intent = new Intent();
		intent.setClass(SQLiteDemo.this, AddActivity.class);
		startActivity(intent);
	}
	
	public void gotoUpdateActivity(){
		int id = 0;//todo
		Intent intent = new Intent(SQLiteDemo.this, UpdateActivity.class);
		Bundle bundle=new Bundle();
		bundle.putString("name", "This is from MainActivity!");
		bundle.putInt("ID", id);
		intent.putExtras(bundle); 
		startActivityForResult(intent, 0);
	}
	
	public void delete(){
		Log.i(TAG, "start delete");
		int personCount = personIdList.size();
		Integer[] personId = new Integer[personCount];
		for (int i = 0; i < personCount; i++) {
			personId[i] = personIdList.get(i);
		}
		personService.delete(personId);
		Intent intent = new Intent();
		intent.setClass(SQLiteDemo.this, SQLiteDemo.class);
		startActivity(intent);
		Log.i(TAG, "end delete");
	}

	
	
	/**
	 * 
	 * @param tblLayout
	 * @param personList
	 */
	public void addToTable(TableLayout tblLayout, List<Person> personList){
		
		personList = personService.getScrollData(0,3,tvName.getText().toString(),tvPhone.getText().toString());
		tblLayout.removeAllViews();
		
		TableRow trTitle = new TableRow(this.getBaseContext());
		
		CheckBox cb = new CheckBox(this.getBaseContext());
		trTitle.addView(cb);
		TextView idTitle = new TextView(this.getBaseContext());
		idTitle.setText("ID");
		idTitle.setTextSize(15);
		idTitle.setPadding(20, 5, 20, 5);
		
		TextView nameTitle = new TextView(this.getBaseContext());
		nameTitle.setText("Name");
		nameTitle.setTextSize(15);
		nameTitle.setPadding(20, 5, 20, 5);
		
		TextView idPhone = new TextView(this.getBaseContext());
		idPhone.setText("Phone");
		idPhone.setTextSize(15);
		idPhone.setPadding(20, 5, 20, 5);
		
		trTitle.addView(idTitle);
		trTitle.addView(nameTitle);
		trTitle.addView(idPhone);
//		trTitle.setLayoutParams(TV_LP);
		trTitle.setBackgroundColor(Color.RED);
		//trTitle.setPadding(20, 5, 20, 5);

		tblLayout.addView(trTitle);
		
		int index = 0;
		for(Person person :personList){			
			Integer id = person.getId();
			String name = person.getName();
			String phone = person.getPhone();
			
			Log.i(TAG, "personid="+id+"name="+name+" phone="+phone);
			
			TableRow tr = new TableRow(this.getBaseContext());
			
			CheckBox item = new CheckBox(this.getBaseContext());

			item.setId(id);
			item.setOnCheckedChangeListener(ocListener);
			tr.addView(item);
			
			TextView idTV = new TextView(this.getBaseContext());
			idTV.setText(String.valueOf(id));
			idTV.setTextSize(15);
			idTV.setPadding(20, 5, 20, 5);
			
			TextView nameTV = new TextView(this.getBaseContext());
			nameTV.setText(name);
			nameTV.setTextSize(15);
			nameTV.setPadding(20, 5, 20, 5);			
			
			TextView phoneTV = new TextView(this.getBaseContext());
			phoneTV.setText(phone);
			phoneTV.setTextSize(15);
			phoneTV.setPadding(20, 5, 20, 5);
			
			tr.addView(idTV);
			tr.addView(nameTV);
			tr.addView(phoneTV);
			
			tr.setLayoutParams(TR_LP);
			if(index++%2==0){
				tr.setBackgroundColor(Color.YELLOW);	
			}else{
				tr.setBackgroundColor(Color.CYAN);	
			}
			
			tr.setPadding(1, 1, 1, 1);		
			tblLayout.addView(tr);
		}
		
		updateBtn.setVisibility(1);
		deleteBtn.setVisibility(1);
		
	}
	
	/**
	 * 
	 * @param tblLayout
	 */
	public void clnResultAndCondition(TableLayout tblLayout){
		tblLayout.removeAllViews();
		tvName.setText(null);
		tvPhone.setText(null);
	}
}